package com.dhcc.core.modules.system.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.cache.dict.DictCache;
import com.dhcc.core.modules.system.cache.dict.IDictCache;
import com.dhcc.core.modules.system.entity.DictCategory;
import com.dhcc.core.modules.system.service.IDictCategoryService;
import com.dhcc.core.modules.system.warpper.DictCategoryWarpper;


/**
 * 字典分类
 * 
 * @ClassName: DictCategoryController
 * @Description: TODO
 * @author cyf
 * @date 2018-08-10 10:22:38
 */
@Controller
@RequestMapping("${sys.backendPath}/dictcategory")
public class DictCategoryController extends BaseController {

	private String PREFIX = "/backend/system/dict/";

	@Autowired
	private IDictCategoryService dictCategoryService;
	
    /**
     * 跳转到添加字典分类
     */
    @RequestMapping("/to_add")
    @Permission("dict:category:add")
    public String dictcategoryAdd() {
        return PREFIX + "category_add.html";
    }
	
	/**
     * 跳转到修改字典分类
     */
    @RequestMapping("/to_update/{dictCategoryId}")
    @Permission("dict:category:update")
    public String dictcategoryUpdate(@PathVariable Long dictCategoryId, Model model) {
        DictCategory dictCategory = this.dictCategoryService.selectById(dictCategoryId);
        model.addAttribute("dictCategory",new DictCategoryWarpper(dictCategory).warp());
        return PREFIX + "category_edit.html";
    }

    /**
     * 新增字典分类
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("dict:category:add")
    @BussinessLog(value = "新增字典分类")
    @CacheClear(IDictCache.CACHE_NAME)
    public Object add(DictCategory dictCategory) {
        if (CommonUtil.isOneEmpty(dictCategory)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        dictCategory.init();
        dictCategory.insert();
        return R.ok();
    }

    /**
     * 删除字典分类
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("dict:category:delete")
    @BussinessLog(value = "删除字典分类")
    @CacheClear(IDictCache.CACHE_NAME)
    public Object delete(@RequestParam Long dictCategoryId) {
	
        this.dictCategoryService.deleteById(dictCategoryId);

        return R.ok();
    }

    /**
     * 修改字典分类
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("dict:category:update")
    @BussinessLog(value = "修改字典分类")
    @CacheClear(IDictCache.CACHE_NAME)
    public Object update(DictCategory dictCategory) {
        if (CommonUtil.isOneEmpty(dictCategory)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        DictCategory old = this.dictCategoryService.selectById(dictCategory.getId());

        old.setName(dictCategory.getName());
        old.setParentId(dictCategory.getParentId());
        old.setDescription(dictCategory.getDescription());
        old.init();
        old.setWeight(dictCategory.getWeight());
        old.updateById();
        return R.ok();
    }


    /**
     * 获取字典分类的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> categoryTree() {
        return DictCache.me().getCategoryTree();
    }
	
}
